package com.paomi.client.fragment.shop;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseViewHolder;
import com.makeramen.roundedimageview.RoundedImageView;
import com.paomi.client.GlideApp;
import com.paomi.client.R;
import com.paomi.client.activity.detail.DetailTicketActivity;
import com.paomi.client.base.BaseLCEFragment;
import com.paomi.client.fragment.first.model.TicketJson;
import com.paomi.client.fragment.shop.presenter.ShopFraPre;
import com.paomi.client.utils.Util;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;

/**
 * Created by pc on 2018/10/25
 * Describe:周边的搜索列表显示
 * com.paomi.client.fragment
 */
public class ShopFragment extends BaseLCEFragment<TicketJson, ShopFraPre> {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private String getData="";
    private String classifyid="0";
    private boolean get;//是否刷新
    private int pageNum = 1;
    private String  check;
    @BindView(R.id.ptr_refresh)
    PtrClassicFrameLayout ptrRefresh;
    private List<TicketJson> ticketList=new ArrayList<>();

    public static ShopFragment newInstance(String name) {

        Bundle args = new Bundle();
        args.putString("name", name);
        ShopFragment fragment = new ShopFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @NonNull
    @Override
    protected int getFragmentLayoutId() {
        return R.layout.layout_only_rec;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        setPtrFrameLayout(ptrRefresh);
        initAdapter();
        loadData(true);
    }

    @NonNull
    @Override
    protected RecyclerView createRecycler() {
        return recyclerView;
    }

    @Override
    protected int getItemLayout() {
        return R.layout.item_classify_shop;
    }

    @Override
    protected void initEmpty(TextView empty) {
        empty.setTextSize(15);
        empty.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.img_category_empty,0,0);
        empty.setText(getResources().getString(R.string.empty_msg));
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    protected void convert(BaseViewHolder helper, TicketJson item) {
        RoundedImageView performShow=helper.itemView.findViewById(R.id.perform_iv);
//        Util.setWidthAndHeight(performShow, -1, (int) (Util.getDisplay(getActivity()).widthPixels * 0.448 * 1.46));
        GlideApp.with(this).load(item.getThumb()).placeholder(R.mipmap.img_default_goods).into(performShow);
        helper.setText(R.id.shop_name,item.getName());
        helper.setText(R.id.shop_date,item.getDate());
        helper.setText(R.id.shop_money,"¥"+item.getPrice());
        helper.setText(R.id.num_pays,item.getPay_number()+"人付款");
        helper.addOnClickListener(R.id.shop_btn);
    }

    @Override
    protected void onItemClick(View view, TicketJson item) {
        Intent intent=new Intent(getActivity(),DetailTicketActivity.class);
        intent.putExtra("getId",""+item.getId());
        startActivity(intent);
    }

    @Override
    public ShopFraPre createPresenter() {
        return new ShopFraPre();
    }
    public void setListData(){
        Bundle bundle = getArguments();
        if (bundle != null) {
            getData = bundle.getString("getData");
            classifyid = bundle.getString("classify_id");
            get = bundle.getBoolean("classify_get");
            check = bundle.getString("checkBox");
            Log.e("check",check);
            if (get) {
                //网络请求
//                getPresenter().setShopData(pageNum, getData, classifyid);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ptrRefresh.refreshComplete();
                    }
                },1000);
            }

        }
    }

    @Override
    public void setData(List<TicketJson> data, boolean pullToRefresh) {
        super.setData(data, pullToRefresh);
        if(pageNum==1){
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    ptrRefresh.refreshComplete();
                }
            },1000);
        }
        if(data.size()<=9){//移除FooterView
            getmAdapterWrapper().setLoadItemVisibility(false);
        }
        getmLoadMoreHelper().setLoadMoreFinish();
        getmAdapterWrapper().notifyDataSetChanged();
    }
}
